package br.com.gerenteweb.facesConverter;

import java.io.Serializable;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import br.com.gerenteweb.beans.Loja;
import br.com.gerenteweb.daos.LojaDao;

@FacesConverter(value = "lojaConverter", forClass = Loja.class)
public class LojaConverter implements Converter, Serializable {

	@Override
	public String getAsString(FacesContext facesContext,
			UIComponent uiComponent, Object object) {
		Loja loja = new Loja();

		String retorno = "";
		if (object != null) {
			loja = (Loja) object;
			retorno = String.valueOf(loja.getId());
		}
		return retorno;
	}

	@Override
	public Loja getAsObject(FacesContext facesContext, UIComponent uiComponent,
			String value) {

		try {
			return new LojaDao().getPorID(Long.parseLong(value));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}